package grisar.gris.struct.ballistics;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import grisar.gris.R;
import grisar.gris.helpers.DataHolder;
import grisar.gris.helpers.PhotoShop;
import grisar.gris.helpers.ResourceAllocator;
import grisar.gris.struct.I_Ballistic;
import grisar.gris.struct.I_Drawable;
import grisar.gris.struct.I_GameObject;
import grisar.gris.struct.I_Updateable;
import grisar.gris.struct.enemy.I_Enemy;

public class TargetingMissile implements I_Ballistic, I_GameObject, I_Updateable, I_Drawable {
	
	private float level;
	private I_Enemy target;
	private float speed;
	private Point position				= 	new Point( 0,0 );
	private Bitmap raster;	
	private float splashDamage 			= 	0;
	private float splashRadius			= 	0;
	private float explosivePower		= 	10;
	private float initialSpeed			= 	2;
	private float acceleration			= 	0;
	private float maxSpeed				= 	5;
	private Paint nonsensePaint			= 	new Paint();
	
	public float getSplashDamage() { 		return splashDamage; }
	public float getSplashRadius() { 		return splashRadius; }
	public float getExplosivePower() { 		return explosivePower; }
	public float getInitialSpeed() { 		return initialSpeed; }
	public float getAcceleration() { 		return acceleration; }
	public float getMaxSpeed() { 			return maxSpeed; }
	public Point getPosition() { 			return position; }
	public void  setPosition(Point pos) { 	position = pos; }
	
	public TargetingMissile( int lvl, I_Enemy target ) {
		
		//Set the level of this missile, higher level
		//allows a multiplication of all stats.
		level = lvl;
		
		//Reference the target
		this.target = target;
		
		//Set initial speed
		speed = initialSpeed;
		
		raster = ResourceAllocator.getDrawable( R.drawable.rocket, true );
	}
	
	public void update() {
		
		//Check to see if the target is still alive
		if( target == null ) {
			
			if( DataHolder.getEnemies().size() != 0 )
				target = DataHolder.getEnemies().get(0);
			
			//Do nothing
			return;
		}
		
		//Increase speed
		if( speed < maxSpeed )
			speed += acceleration;
		
		//Cut excess speed off, in case the speed limit is passed
		if( speed > maxSpeed )
			speed = maxSpeed;
		
		//Calculate distances
		float xDist = Math.abs( target.getPosition().x - position.x );
		float yDist = Math.abs( target.getPosition().y - position.y );
		
		//Steer the missile, x-axis
		if( target.getPosition().x > position.x )
			position.x += (xDist / 100) * level;
		else
			position.x -= (xDist / 100) * level;
		
		//Steer the missile, y-axis
		if( target.getPosition().y > position.y )
			position.x -= (yDist / 100) * level;
		else
			position.x += (yDist / 100) * level;
	}
	
	public void draw(Canvas canvas) {
		
		//System.out.println( "targetingMissile : " + position.toString() );
		
		//canvas.drawBitmap( raster, position.x, position.y, nonsensePaint );
		
		//int angle = (int) Math.round( angleBetweenPoints( target.getPosition(), position ) );
		
		//PhotoShop.rotate( canvas, angle, raster, position );
		
	}
	
	public static double angleBetweenPoints( Point a, Point b ) {
		
		return Math.atan2( a.x - b.x, a.y - b.y ) * (180 / Math.PI);
	}
}